package com.deer.wms.inventory.service.impl;

//import com.deer.wms.base.system.model.TaskLog;
//import com.deer.wms.base.system.model.container.ContItem;
//import com.deer.wms.base.system.model.container.ContItemDto;
//import com.deer.wms.base.system.service.ContItemService;
//import com.deer.wms.base.system.service.TaskLogService;

import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.dao.StockTakeLogMapper;
import com.deer.wms.inventory.model.stockTake.StockTakeLog;
import com.deer.wms.inventory.model.stockTake.StockTakeLogDto;
import com.deer.wms.inventory.model.stockTake.StockTakeMaster;
import com.deer.wms.inventory.model.stockTake.StockTakeTask;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.inventory.service.StockTakeLogService;
import com.deer.wms.inventory.service.StockTakeMasterService;
import com.deer.wms.inventory.service.StockTakeTaskService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.project.root.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by csl on 2021/04/13.
 */
@Service
@Transactional
public class StockTakeLogServiceImpl extends SuperServiceImpl<StockTakeLogMapper, StockTakeLog> implements StockTakeLogService {

    @Autowired
    private StockTakeLogMapper stockTakeLogMapper;

    @Autowired
    private StockTakeTaskService stockTakeTaskService;

    @Autowired
    private StockTakeMasterService stockTakeMasterService;

    @Autowired
    private InventoryService inventoryService;
    
    @Autowired
    private StockTakeLogService stockTakeLogService;

//    @Autowired
//    private TaskLogService taskLogService;

//    @Autowired
//    private ContItemService contItemService;

    /**
     * 保存盘点记录
     * @param stockTakeLogDto
     * @param currentUser
     */
    @Override
    public void saveLog(StockTakeLogDto stockTakeLogDto, CurrentUser currentUser) {
        if (stockTakeLogDto.getItemList() != null){
            List<Map<String,Object>> itemList = stockTakeLogDto.getItemList();
            if (itemList.size()>0){
                for (Map<String,Object> item : itemList){
                    StockTakeLog stockTakeLog = new StockTakeLog();
                    stockTakeLog.setStockMasterId(stockTakeLogDto.getStockTakeMasterId());
                    stockTakeLog.setStockMasterCode(stockTakeLogDto.getStockMasterCode());
                    stockTakeLog.setStockTaskId(stockTakeLogDto.getStockTaskId());
                    stockTakeLog.setStockTaskCode(stockTakeLogDto.getTaskBillNo());
                    stockTakeLog.setCellCode(stockTakeLogDto.getCellCode());
                    stockTakeLog.setContCode(stockTakeLogDto.getBoxCode());
                    stockTakeLog.setWareId(stockTakeLogDto.getWareId());
                    stockTakeLog.setTaskUserId(currentUser.getUserId());
                    stockTakeLog.setTaskUserName(currentUser.getUserName());
                    stockTakeLog.setTaskTime(DateUtils.getNowDateTimeString());
                    stockTakeLog.setItemCode(String.valueOf(item.get("itemCode")));
                    stockTakeLog.setItemName(String.valueOf(item.get("itemName")));
                    if (item.get("itemPc")!=null){
                        stockTakeLog.setItemPc(String.valueOf(item.get("itemPc")));
                    }
                    stockTakeLog.setQuantity(Double.parseDouble(String.valueOf(item.get("quantity"))));
                    stockTakeLog.setTaskQuantity(Double.parseDouble(String.valueOf(item.get("taskQuantity"))));
                    Double residualQuantity = Double.parseDouble(String.valueOf(item.get("taskQuantity"))) - Double.parseDouble(String.valueOf(item.get("quantity")));
                    if (residualQuantity>0){
                        stockTakeLog.setProfitOrLoss(1);
                    }else if (residualQuantity<0){
                        stockTakeLog.setProfitOrLoss(-1);
                    }else {
                        stockTakeLog.setProfitOrLoss(0);
                    }
                    stockTakeLog.setResidualQuantity(residualQuantity);
                    stockTakeLogService.save(stockTakeLog);
                }
                //保存成功之后修改盘点状态
//                StockTakeTask stockTakeTask = new StockTakeTask();
                StockTakeTask stockTakeTask = stockTakeTaskService.getById(stockTakeLogDto.getStockTaskId());

//                stockTakeTask.setStockTakeTaskId(stockTakeLogDto.getStockTaskId());
//                stockTakeTask.setStockTakeMasterId(stockTakeLogDto.getStockTakeMasterId());
//                stockTakeTask.setTaskBillNo(stockTakeLogDto.getTaskBillNo());
//                stockTakeTask.setCellCode(stockTakeLogDto.getCellCode());
                stockTakeTask.setBoxCode(stockTakeLogDto.getBoxCode());
                stockTakeTask.setTaskTime(DateUtils.getNowDateTimeString());
                stockTakeTask.setTaskUserName(currentUser.getUserName());
                stockTakeTask.setState(2);
                stockTakeTaskService.updateById(stockTakeTask);
                //查询该盘点任务号下所有下架任务是否都完成
                Integer num = stockTakeTaskService.selectNum(stockTakeLogDto.getStockTakeMasterId());
                if (num == 0){//表示全部完成，修改盘点任务状态为已完成
//                    StockTakeMaster stockTakeMaster = new StockTakeMaster();
                    StockTakeMaster stockTakeMaster = stockTakeMasterService.getById(stockTakeLogDto.getStockTakeMasterId());
//                    stockTakeMaster.setStockTakeMasterId(stockTakeLogDto.getStockTakeMasterId());
                    stockTakeMaster.setState(1);
                    stockTakeMasterService.updateById(stockTakeMaster);
                }
            }
        }
        else {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"数据错误");
        }

    }

      //数据库中有，实际库存没有  盘亏
//    private void setLog(StockTakeLogDto stockTakeLogDto,CurrentUser currentUser,ContItemDto contItemDto){
//        StockTakeLog stockTakeLog = new StockTakeLog();
//        stockTakeLog.setStockMasterId(stockTakeLogDto.getStockMasterId());
//        stockTakeLog.setStockMasterCode(stockTakeLogDto.getStockMasterCode());
//        stockTakeLog.setStockTaskId(stockTakeLogDto.getStockTaskId());
//        stockTakeLog.setStockTaskCode(stockTakeLogDto.getStockTaskCode());
//        stockTakeLog.setCellCode(stockTakeLogDto.getCellCode());
//        stockTakeLog.setContCode(stockTakeLogDto.getContCode());
//        stockTakeLog.setWareId(stockTakeLogDto.getWareId());
//        stockTakeLog.setTaskUserId(currentUser.getUserId());
//        stockTakeLog.setTaskUserName(currentUser.getUserName());
//        stockTakeLog.setTaskTime(DateUtils.getNowDateTimeString());
//        stockTakeLog.setContItemId(contItemDto.getId());
//        stockTakeLog.setItemCode(contItemDto.getItemInfo().getItemCode());
//        stockTakeLog.setItemName(contItemDto.getItemInfo().getItemName());
//        if (contItemDto.getBatchNumber() != null){
//            stockTakeLog.setItemPc(contItemDto.getBatchNumber());
//        }
//        if (contItemDto.getBuildNumber() != null){
//            stockTakeLog.setBuildNumber(contItemDto.getBuildNumber());
//        }
//        stockTakeLog.setQuantity(contItemDto.getQty());
//        stockTakeLog.setTaskQuantity(0.0);
//        stockTakeLog.setProfitOrLoss(-1);
//        stockTakeLog.setResidualQuantity(0.0-contItemDto.getQty());
//        save(stockTakeLog);
//    }
    
    
    /**
     * 根据盘点下架任务号查询盘点记录
     * @param wareId
     * @param cellCode
     * @return
     */
    @Override
    public List<StockTakeLog> findList(Integer wareId, String cellCode) {
        return stockTakeLogMapper.findList(wareId,cellCode);
    }

    /**
     * 根据盘点任务号查询盘点差异表
     * @param wareId
     * @param stockMasterCode
     * @return
     */
    @Override
    public List<StockTakeLog> findResidualList(Integer wareId, String stockMasterCode) {
        return stockTakeLogMapper.findResidualList(wareId,stockMasterCode);
    }

    /**
     * 根据盘点任务号查询盘点盈亏记录
     * @param wareId
     * @param stockMasterCode
     * @param profitOrLoss
     * @return
     */
    @Override
    public List<StockTakeLog> findProfitOrLossList(Integer wareId, String stockMasterCode, Integer profitOrLoss) {
        return stockTakeLogMapper.findProfitOrLossList(wareId,stockMasterCode,profitOrLoss);
    }
}
